"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
const taro_1 = require("@tarojs/taro");
const taro_ui_1 = require("taro-ui");
const components_1 = require("@tarojs/components");
const redux_1 = require("@tarojs/redux");
const XPage_1 = require("../../components/XPage");
const cloverActive_2x_png_1 = require("../../assets/cloverActive@2x.png");
const my_2x_png_1 = require("./my@2x.png");
const my_1 = require("../../actions/my");
require("./index.scss");
const prefix = 'http://127.0.0.1:7001';
let My = class My extends taro_1.Component {
    constructor() {
        super(...arguments);
        this.config = {
            navigationBarTitleText: '幸运草小程序'
        };
        this.state = {
            isToastOpened: false,
            isToastTitle: '请先登录！',
            data: {}
        };
        // 登录
        this.onGotUserInfo = e => {
            // 第一步 登录成功后获得用户信息
            const data = e.detail.rawData ? JSON.parse(e.detail.rawData) : {};
            // 第二步 保存到 store
            this.setState(state => (Object.assign({}, state, { data })), () => {
                // 第三步 换取 openId并完成注册
                taro_1.default.login().then((result) => {
                    const obj = {};
                    Object.assign(obj, result, data, { type: 0 });
                    taro_1.default.request({
                        url: `${prefix}/api/v1/access/weapp`,
                        method: 'POST',
                        data: obj,
                        header: { 'content-type': 'application/json' }
                    })
                        .then(res => {
                        taro_1.default.setStorage({
                            key: 'userInfo',
                            data: Object.assign({}, res.data.data)
                        });
                    });
                });
            });
        };
        this.handleToPage = (pathName, e) => {
            e.stopPropagation();
            taro_1.default.getSetting().then(e => {
                if (e.authSetting['scope.userInfo']) {
                    taro_1.default.navigateTo({ url: pathName });
                }
                else {
                    taro_1.default.showToast({ title: '请先登录！', icon: 'none' });
                }
            });
        };
        this.handleChooseAddress = () => {
            taro_1.default.getSetting().then(e => {
                if (e.authSetting['scope.userInfo']) {
                    const userInfo = taro_1.default.getStorageSync('userInfo');
                    // 提交收货地址
                    taro_1.default.chooseAddress()
                        .then(addressInfo => {
                        this.props.addAddress(Object.assign({}, userInfo, { addressInfo }), () => { });
                    });
                }
                else {
                    taro_1.default.showToast({ title: '请先登录！', icon: 'none' });
                }
            });
        };
        this.handleIntegralIns = () => {
            taro_1.default.getSetting().then(e => {
                if (e.authSetting['scope.userInfo']) {
                    this.props.add(({ number: 2 }), data => {
                        if (data.errno) {
                            taro_1.default.showToast({ title: data.errmsg, icon: 'none' });
                        }
                        else {
                            taro_1.default.showToast({ title: data.errmsg, icon: 'success' });
                        }
                    });
                }
                else {
                    taro_1.default.showToast({ title: '请先登录！', icon: 'none' });
                }
            });
        };
    }
    componentWillMount() {
        taro_1.default.getSetting().then(e => {
            if (e.authSetting['scope.userInfo']) {
                taro_1.default.getUserInfo().then(userInfo => {
                    this.setState(state => (Object.assign({}, state, { data: userInfo.rawData ? JSON.parse(userInfo.rawData) : {} })));
                });
            }
        });
    }
    render() {
        const dashboardArray = [{
                number: 6,
                title: '全部抽奖',
                pathName: '/pages/my/goods/index?userId=1'
            }, {
                number: 0,
                title: '发起抽奖',
                pathName: '/pages/my/goods/launch/index'
            }, {
                number: 1,
                title: '中奖纪录',
                pathName: '/pages/my/goods/Records'
            }];
        return (<XPage_1.default>
        <components_1.View className='my'>
          <components_1.View className='bg'>
            <components_1.Image src={my_2x_png_1.default} mode='widthFix' className='bg-image'/>
            <components_1.View className='position'>
              <components_1.View className='collect'>
                <components_1.View className='info'>
                  <components_1.Button openType="getUserInfo" onGetUserInfo={this.onGotUserInfo} className="getUserInfo">
                    {this.props.data.avatarUrl
            ?
                <taro_ui_1.AtAvatar circle image={this.props.data.avatarUrl}/>
            :
                <components_1.View className="authorize">登录</components_1.View>}
                  </components_1.Button>
                  
                  <components_1.View className='mine'>
                    <components_1.Text className='userName'>{this.props.data.nickName ? this.props.data.nickName : '请先登录'}</components_1.Text>
                    
                  </components_1.View>
                </components_1.View>
                <components_1.View className='progress'>
                  <components_1.View className='clover'>
                    {Array.from(new Array(6)).map((item, key) => {
            return (<components_1.View className='cell' key={key}>
                            <components_1.Image src={cloverActive_2x_png_1.default} mode='widthFix' className='clover-icon'/>
                          </components_1.View>);
        })}
                  </components_1.View>
                  <components_1.View className='explain'>
                    <components_1.Text className='text'>
                       四叶草每小时产出，共6枚，记得收取哦~
                    </components_1.Text>
                  </components_1.View>
                  <components_1.View className='submit'>
                    <taro_ui_1.AtButton type='primary' size='small' className='btn' onClick={this.handleIntegralIns.bind(this)}>收获</taro_ui_1.AtButton>
                  </components_1.View>
                </components_1.View>
              </components_1.View>
            </components_1.View>
          </components_1.View>

          <components_1.View className='table_1543304811640'>
            {dashboardArray.map((item, key) => (<components_1.View className='cell' key={key} onClick={this.handleToPage.bind(this, item.pathName)}>
                  
                  <components_1.Text className='secondary'>{item.title}</components_1.Text>
                </components_1.View>))}
          </components_1.View>

        </components_1.View>
        <taro_ui_1.AtList>
          <taro_ui_1.AtListItem title="收货地址" arrow='right' 
        // thumb="http://piy5kv2h2.bkt.clouddn.com/position1.svg"
        onClick={this.handleChooseAddress}/>
          <taro_ui_1.AtListItem title="常见问题" arrow='right' 
        // thumb="http://piy5kv2h2.bkt.clouddn.com/help1.svg"
        onClick={this.handleToPage.bind(this, "/pages/my/Questions")}/>
          <taro_ui_1.AtListItem title="意见反馈" arrow='right' 
        // thumb="http://piy5kv2h2.bkt.clouddn.com/feedback1.svg"
        onClick={this.handleToPage.bind(this, "/pages/my/Feedbacks")}/>
        </taro_ui_1.AtList>
      </XPage_1.default>);
    }
};
My = __decorate([
    redux_1.connect(({ my }) => ({
        my
    }), (dispatch) => ({
        add(data, callback) {
            dispatch(my_1.add(data, callback));
        },
        addAddress(data, callback) {
            dispatch(my_1.addAddress(data, callback));
        }
    }))
], My);
exports.default = My;
//# sourceMappingURL=index.jsx.map